#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char const *argv[])
{
        if(argc != 2){
                printf("./a.out mem.txt\n");
                return -1;
        }
        int fd = open(argv[1], O_RDWR);
        int* mem = (int *)mmap(NULL,4,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
        if(mem == MAP_FAILED){
                perror("mmap err");
                return -1;
        }
        pid_t pid = fork();
        if(pid == 0){
                *mem = 100;
                printf("child,*mem=%d\n",*mem);
                sleep(3);
                printf("child,*mem=%d\n",*mem);
        }else if(pid > 0){
                sleep(1);
                printf("parent,*mem=%d\n",*mem);
                *mem = 1001;
                printf("parent,*mem=%d\n",*mem);
                wait(NULL);
                munmap(mem, 4);
        }
        return 0;
}
       